﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LPV_POS.Baocao;
using CrystalDecisions.CrystalReports.Engine;

namespace LPV_POS.mh_pos.mh_danhmuc
{
    public partial class mh_dmkhachhang_TimKiem_In : Form
    {
        public mh_dmkhachhang_TimKiem_In()
        {
            InitializeComponent();
        }

        private void mh_dmkhachhang_TimKiem_In_Load(object sender, EventArgs e)
        {
            cboThang.SelectedIndex = Variable.ngay_gio_hienhanh.Month - 1;
            
            string sSQL = "";
            sSQL = "SELECT OBJID = NULL, OBJNAME = N' --Nhóm khách hàng--' " +
                    "UNION " +
                    "SELECT dn.OBJID, dn.OBJNAME FROM DM_NHOMKHACHHANG dn";
            thuvien othuvien = new thuvien();
            comboBox1.DataSource = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });

        }
        DataTable tbDM_KHACHHANG = null;
        private void btnTim_Click(object sender, EventArgs e)
        {
            string sSQL = "SELECT K.* FROM DM_KHACHHANG k ";

            sSQL += " WHERE MONTH(k.NGAYSINH) =  " + cboThang.Text + " AND YEAR(k.NGAYSINH) <> 1900 ";

            if (comboBox1.SelectedIndex > 0)
                sSQL += " AND MANHOMKHACHHANG = '" + comboBox1.SelectedValue + "' ";
            

            thuvien othuvien = new thuvien();
            tbDM_KHACHHANG = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            lblMessage.Text = tbDM_KHACHHANG.Rows.Count + " khách hàng";

            btnIn.Enabled = tbDM_KHACHHANG.Rows.Count > 0;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            if (tbDM_KHACHHANG.Rows.Count == 0)
                MessageBox.Show("Không có dữ liệu để in");
            else
            {
                this.Cursor = Cursors.WaitCursor;
                ReportDocument rpt = new ReportDocument();
                this.Cursor = Cursors.AppStarting;
                string sRptPath = Application.StartupPath + "\\report\\rptkhach_hang.rpt";
                rpt.Load(sRptPath, CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy);

                tbDM_KHACHHANG.TableName = "DM_KHACHHANG";
                //tbDM_KHACHHANG.WriteXmlSchema(sRptPath.Replace("rptkhach_hang.rpt", "DSkhach_hang.xsd"));
                
                rpt.SetDataSource(tbDM_KHACHHANG);
                mh_View frm = new mh_View(rpt);
                this.Cursor = Cursors.Default;
                frm.ShowDialog();
            }
        }
    }
}
